package com.hwadee.springboot.controller.ExamController;

import com.hwadee.springboot.entity.Grade;
import com.hwadee.springboot.entity.Mistake;
import com.hwadee.springboot.entity.R;
import com.hwadee.springboot.entity.StudentExam;
import com.hwadee.springboot.mapper.StudentExamMapper;
import com.hwadee.springboot.service.GradeService;
import com.hwadee.springboot.service.impl.MistakeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student/grades")
public class GradeController {

    @Autowired
    private GradeService gradeService;
    @Autowired
    private StudentExamMapper  studentExamMapper;

    @PostMapping
    public R<?> saveGrade(@RequestBody Grade grade,@RequestParam Integer examId) {
        try {
            boolean saved = gradeService.save(grade);
            if (saved) {
                // 保存学生考试关联记录
                StudentExam studentExam = new StudentExam();
                studentExam.setStudentid(grade.getStudentid());
                studentExam.setExamid(examId);
                studentExamMapper.insert(studentExam);
                return R.ok().msg("成绩保存成功").data(grade);
            } else {
                return R.error(500, "成绩保存失败");
            }
        } catch (Exception e) {
            return R.error(500, "成绩保存失败: " + e.getMessage());
        }
    }



}
